package com.agent.service.impl;

import com.agent.entity.Knowledge;
import com.agent.entity.SearchTag;
import com.agent.page.EasyuiPage;
import com.agent.repository.KnowledgeMapper;
import com.agent.repository.SearchTagMapper;
import com.agent.service.IKnowledgeService;
import com.agent.vo.ConditionCount;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;


@Service("knowledgeService")
public class KnowledgeServiceImpl implements IKnowledgeService {
    private Map<String, ConditionCount> top10 = new ConcurrentHashMap<>();

    @Autowired
    SearchTagMapper searchTagMapper;
    @Autowired
    protected KnowledgeMapper knowledgeMapper;

    @PostConstruct
    private void initTop10() {
        List<SearchTag> searchTags = searchTagMapper.searchAll();
        searchTags.forEach(searchTag -> {
            top10.put(searchTag.getTag(),new ConditionCount(searchTag.getTag(),searchTag.getCount()));
        });
    }

    @PreDestroy
    private void destory() {
        top10.forEach((k,v) ->{
            SearchTag searchTag = new SearchTag();
            searchTag.setCount(v.getCount());
            searchTag.setTag(v.getCondition());
            searchTagMapper.savaOrUpdate(searchTag);
        });
    }
    public List<ConditionCount> getCondition(){
        List<ConditionCount> values = top10.values().stream().collect(Collectors.toList());
        Collections.sort(values,(o1,o2)->{
            ConditionCount c1 = (ConditionCount) o1;
            ConditionCount c2 = (ConditionCount) o2;
            return c2.getCount().compareTo(c1.getCount());
        });
        return values.size()>10?values.subList(0,10):values;
    }


    public void delete(String id) {
        knowledgeMapper.delete(id);

    }

    public List<Knowledge> getKnowledgeList(String id) {
        return knowledgeMapper.getKnowledge(id);
    }

    public Knowledge getKnowledge(String id) {
        List<Knowledge> list = knowledgeMapper.getKnowledge(id);
        return list == null || list.size() == 0l ? null : list.get(0);
    }

    public void insert(Knowledge knowledge) {
        knowledgeMapper.insert(knowledge);
    }

    public List<Knowledge> searchAll() {
        return knowledgeMapper.searchAll();
    }

    public EasyuiPage<Knowledge> searchKnowledge(Integer page, Integer rows, Knowledge knowledge) {
        PageHelper.startPage(page, rows, true, true, true);
        List<Knowledge> list = knowledgeMapper.searchKnowledge(knowledge);
        EasyuiPage<Knowledge> pages = new EasyuiPage<Knowledge>();
        pages.setRows(list);
        PageInfo<Knowledge> pageinfo = new PageInfo<Knowledge>(list);
        pages.setTotal(pageinfo.getTotal());
        return pages;
    }

    public List<Knowledge> search(Knowledge knowledge) {
        return knowledgeMapper.searchKnowledge(knowledge);
    }

    public void update(Knowledge Knowledge) {
        knowledgeMapper.update(Knowledge);
    }

    public KnowledgeMapper getKnowledgeMapper() {
        return knowledgeMapper;
    }

    public void setKnowledgeMapper(KnowledgeMapper KnowledgeMapper) {
        this.knowledgeMapper = KnowledgeMapper;
    }

    public List<Knowledge> searchKnowledgeByWeixin(Knowledge knowledge) {
        if(!StringUtils.isEmpty(knowledge.getTag())){
            addConditionToMap(knowledge.getTag());
        }
        return knowledgeMapper.searchKnowledgeByWeixin(knowledge);
    }

    private void addConditionToMap(String condition) {
        if (null == top10.get(condition)) {
            top10.put(condition, new ConditionCount(condition, 1l));
        } else {
            ConditionCount conditionCount = top10.get(condition);
            conditionCount.setCount(conditionCount.getCount() + 1);
            top10.put(condition, conditionCount);
        }
    }


}
